package com.itmg.analyze.jp.data.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.itmg.analyze.jp.bean.AnalyzeBean;
import com.itmg.analyze.jp.data.IDataAnalyze;
import com.itmg.vo.SearchResultItemVO;
import com.itmg.vo.SearchResultVO;
import com.itmg.vo.TargetDataVO;

/**
 *  dom解析rakuten
 * @author xuehaipeng
 * @version 1.0
 * */

public class RakutenAuctionDataAnlyzeImpl implements IDataAnalyze {
	
	protected Logger log = Logger.getLogger(RakutenAuctionDataAnlyzeImpl.class.getName());
	
	public RakutenAuctionDataAnlyzeImpl() {
	}
	
	public SearchResultVO getSearchResultItemVO(TargetDataVO targetDataVO) throws Exception {
		Document theDocument = AnalyzeBean.parseDocument(targetDataVO.getUrl());
		String sourceSiteName = targetDataVO.getSourceSiteName();
		SearchResultVO searchResultVO = new SearchResultVO();
		if(theDocument == null)
			return searchResultVO;
		Element theRootElement = theDocument.getDocumentElement();
		NodeList theFirstNodeList = theRootElement.getElementsByTagName("auctionItemSearch:AuctionItemSearch");
		List<SearchResultItemVO> searchResultItemVOList = new ArrayList<SearchResultItemVO>();
		for(int i=0;i<theFirstNodeList.getLength();i++)
		{
			Node firstItemNode = theFirstNodeList.item(i);
			if(firstItemNode.getNodeName().equals("auctionItemSearch:AuctionItemSearch"))
			{
				NodeList firstNodeList = firstItemNode.getChildNodes();
				for(int j=0;j<firstNodeList.getLength();j++)
				{
					Node secondNode = firstNodeList.item(j);
					if(secondNode.getNodeName().equals("count"))
					{
						String count = secondNode.getTextContent().trim();
						if(!"".equals(count))
						{
							searchResultVO.setTotalResultNumber(Integer.valueOf(count));
						}					
					}
					if(secondNode.getNodeName().equals("Items"))
					{
						NodeList thirdNodeList = secondNode.getChildNodes();
						for(int n=0;n<thirdNodeList.getLength();n++)
						{
							Node thirdNode = thirdNodeList.item(n);
							if(thirdNode.getNodeName().equals("Item"))
							{
								SearchResultItemVO searchResultItemVO = new SearchResultItemVO();
								NodeList nodeItemList = thirdNode.getChildNodes();
								for(int m=0;m<nodeItemList.getLength();m++)
								{
									Node fourthNode = nodeItemList.item(m);
									if(fourthNode.getNodeName().equals("itemName"))
									{
										String ProductName = fourthNode.getTextContent().trim();
								
										if(!"".equals(ProductName))
										{
											searchResultItemVO.setProductName(ProductName);
										}					
									}
									if(fourthNode.getNodeName().equals("itemCode"))
									{
										String productID = fourthNode.getTextContent().trim();
										if(!"".equals(productID))
										{
											searchResultItemVO.setProductID(productID);
										}				
									}
									if(fourthNode.getNodeName().equals("itemPrice"))
									{
										String lowestPrice = fourthNode.getTextContent().trim();
										if(!"".equals(lowestPrice))
										{
											searchResultItemVO.setLowestPrice(Integer.parseInt(lowestPrice));
										}		
									}
									if(fourthNode.getNodeName().equals("itemCaption"))
									{
										String Comment = fourthNode.getTextContent().trim();
										if(!"".equals(Comment))
										{
											searchResultItemVO.setComment(Comment);
										}			
									}
									if(fourthNode.getNodeName().equals("itemUrl"))
									{
										String productUrl = fourthNode.getTextContent().trim();
										if(!"".equals(productUrl))
										{
											searchResultItemVO.setProductUrl(productUrl);
										}				
									}
									if(fourthNode.getNodeName().equals("smallImageUrl"))
									{
										String smallImageUrl = fourthNode.getTextContent().trim();
										if(!"".equals(smallImageUrl))
										{
											searchResultItemVO.setSmallImageUrl(smallImageUrl);
										}			
									}
									if(fourthNode.getNodeName().equals("mediumImageUrl"))
									{
										String mediumImageUrl = fourthNode.getTextContent().trim();
										if(!"".equals(mediumImageUrl))
										{
											searchResultItemVO.setBigImageUrl(mediumImageUrl);
										}			
									}
									if(fourthNode.getNodeName().equals("bidCount"))
									{
										String count = fourthNode.getTextContent().trim();
										if(!"".equals(count))
										{
											searchResultItemVO.setCount(Integer.parseInt(count));
										}				
									}
									if(fourthNode.getNodeName().equals("affiliateRate"))
									{
										String evaluation = fourthNode.getTextContent().trim();
										if(!"".equals(evaluation))
										{
											searchResultItemVO.setEvaluation(Double.parseDouble(evaluation));
										}			
									}
									if(fourthNode.getNodeName().equals("startTime"))
									{
										String startTime = fourthNode.getTextContent().trim();
										if(!"".equals(startTime))
										{
											searchResultItemVO.setSaleStartTime(startTime);
										}			
									}
									if(fourthNode.getNodeName().equals("endTime"))
									{
										String endTime = fourthNode.getTextContent().trim();
										if(!"".equals(endTime))
										{
											searchResultItemVO.setSaleEndTime(endTime);
										}			
									}
									if(fourthNode.getNodeName().equals("shopName"))
									{
										String shopName = fourthNode.getTextContent().trim();
										if(!"".equals(shopName))
										{
											searchResultItemVO.setShopName(shopName);
										}				
									}
								}
								String md5 = searchResultItemVO.getMyProductMd5();
								searchResultItemVO.setProductMd5(md5);
								searchResultItemVO.setSourceSiteName(sourceSiteName);
								searchResultItemVOList.add(searchResultItemVO);
							}
						}
					}
				}
			}
		}	
		searchResultVO.setResultItemList(searchResultItemVOList);
		return searchResultVO; 
	} 
}
